﻿using PdfiumViewer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfRender
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //new PdfConverter().ConvertToPdf(@"D:\input.docx", @"D:\output.pdf");
            ConvertPdfToImages(@"D:\output.pdf", @"D:\output\");
            Console.WriteLine("Done!");
            Console.ReadLine();

            // 加载 Word 文档
            //Document document = new Document("D:\\Work\\other\\A\\PdfRender20250214\\PdfRender\\input.docx");

            //// 设置保存选项
            //SaveOptions saveOptions = new PdfSaveOptions
            //{
            //    SaveFormat = SaveFormat.Pdf,
            //    // 可以选择输出为每页一张图片
            //    PageSavingCallback = new ImagePageSavingCallback()
            //};

            //// 导出为 PDF
            //document.Save("d:\\output.pdf", saveOptions);
        }

        public static List<string> ConvertPdfToImages(string pdfPath, string outputDir)
        {
            var imagePaths = new List<string>();
            using (var doc = PdfDocument.Load(pdfPath))
            {
                for (int i = 0; i < doc.PageCount; i++)
                {
                    using (var image = doc.Render(i, 300, 300, true))
                    {
                        string path = Path.Combine(outputDir, $"page_{i}.png");
                        image.Save(path, ImageFormat.Png);
                        imagePaths.Add(path);
                    };
                }
            }

            return imagePaths;
        }
    }

    //public class ImagePageSavingCallback : IPageSavingCallback
    //{
    //    public void PageSaving(PageSavingArgs args)
    //    {
    //        // 可以在此处处理每页保存后的逻辑 例如将每页转换为图片或其他处理
    //        Console.WriteLine("Page saved: " + args.PageIndex);
    //    }
    //}
}